package cn.wolfcode.cloud.shop.web.controller;

import cn.wolfcode.cloud.shop.common.Result;
import cn.wolfcode.cloud.shop.domain.User;
import cn.wolfcode.cloud.shop.mapper.UserMapper;
import cn.wolfcode.cloud.shop.service.IUserServer;
import cn.wolfcode.cloud.shop.util.CookieUtil;
import cn.wolfcode.cloud.shop.web.result.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @Description: 用户功能测试
 * @Author: 伍家钰
 * @Date: 2020/9/5 19:51
 * @Version: 1.0
 */
@RestController
public class UserController {
    @Autowired
    private IUserServer userServer;

    @RequestMapping("get")
    public String get(Long id) {
        User user = userServer.selectById(id);
        return user.toString();
    }

    @RequestMapping("doLogin")
    public Result login(@Valid LoginVo vo, HttpServletResponse response) {
        String token = userServer.login(vo);
        // 将token以cookie的形式返回给浏览器
        CookieUtil.addcookie("userToken", "token", 1800, response);
        return Result.success(null);
    }
}
